/******************************************************************************中
 * @brief: file  : systask.c
 * @Author:   aple
 * @Date: 2022-12-14 18:06:21
 * @LastEditors: aple
 * @LastEditTime: 2023-02-27 16:06:54
 * @Verision: 1.0.0.0
 ******************************************************************************/
#include "os_obj.h"
#include "sys_task.h"
#include "pincfg.h"
#include "common.h"
#include "bsp_io++.h"
#include "mix_log.h"
#include "nor_cmd.h"
#include "lib_surport.h"

/*****************************************************************************
 * @brief   fun.
 *****************************************************************************/
static void Erase(uint32_t AddrBase);
static void Program(uint32_t Addr, uint8_t *dat, uint16_t Len);
static void Read(uint32_t Addr, uint8_t *dat, uint16_t Len);
int mix_log_i(int argc, char **argv);

/*****************************************************************************
 * @brief   var.
 *****************************************************************************/
mix_log_api_ref(mix_log_obj);
mix_log_info_type mix_log_info;
mix_log_type mix_log_obj = {
	256 * 1024,
	128 * 1024,
	4 * 1024,
	64,
	&mix_log_info,
	Erase,
	Program,
	Read,
	mix_log_api_name(mix_log_obj)};
mix_log_api_make(mix_log_obj);

TIME_Type log_tm;

static void Erase(uint32_t AddrBase)
{
	nor_sector_erase(AddrBase);
}
static void Program(uint32_t Addr, uint8_t *dat, uint16_t Len)
{
	nor_write_multi(Addr, Len, dat);
}
static void Read(uint32_t Addr, uint8_t *dat, uint16_t Len)
{
	nor_read(Addr, Len, dat);
}

/*****************************************************************************
 * @brief   fun.
 *****************************************************************************/
extern "C"
{
	void mix_log_demo_init(void);
}

/*****************************************************************************
 * @brief   system task.
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
obj_init_order5(mix_log_demo_init);
void mix_log_demo_init(void)
{
	mix_log_i(1, 0);
}

int mix_log_i(int argc, char **argv)
{
	mix_log_init(&mix_log_obj);

	dbg_print("log info:start=%d, blk=%d:sit=%d:addr=%d" PTCR,
			  mix_log_obj.info->page_start,
			  mix_log_obj.info->page_now,
			  mix_log_obj.info->site_next,
			  mix_log_obj.info->dat_addr_last);

	return 0;
}
DBG_FUN_EXPORT(mix_log_i);

int mix_log_c(int argc, char **argv)
{
	int blks = mix_log_obj.total_size / mix_log_obj.page_size;

	while (blks--)
	{
		nor_sector_erase(mix_log_obj.addr_base + mix_log_obj.page_size * blks);
	}

	mix_log_i(1, 0);

	dbg_print("log clr" PTCR);

	return 0;
}
DBG_FUN_EXPORT(mix_log_c);

int mix_log_w(int argc, char **argv)
{
	sig_time_type sigtm;
	int cnt = 1;
	uu16 len;
	ii32 dat;
	uu08 buff[128];

	if (argc < 2)
		return 1;

	if (argc >= 3)
		cnt = atoi(argv[2]);

	dat = atoi(argv[1]);
	len = 30;

	while (cnt--)
	{
		int index = 0;
		var_loop_inc(len, 24, 64);

		rtc_time_run_sec(&log_tm, 10);

		sigtm = time_to_sig(&log_tm);

		index = sprintf((char *)buff, "mix log: %08X", dat);
		memset(&buff[index], '+', len - index + 1);

		mix_log_save(&mix_log_obj, sigtm.val, buff, len);

		dat++;
	}

	dbg_print("log save:%d:%s" PTCR, strlen(argv[1]), argv[1]);
	dbg_print("log info:blk=%3d:sit=%3d:addr=%4d" PTCR, mix_log_obj.info->page_now, mix_log_obj.info->site_next, mix_log_obj.info->dat_addr_last);

	return 0;
}
DBG_FUN_EXPORT(mix_log_w);

int mix_log_r(int argc, char **argv)
{
	mix_log_obj_type logr;
	sig_time_type sigtm;
	uu8 buff[128];

	if (argc < 3)
		return 1;

	memset(buff, 0, sizeof(buff));
	logr.dat = buff;
	logr.page_site = atoi(argv[1]);
	logr.sub_site = atoi(argv[2]);

	mix_log_read(&mix_log_obj, &logr);
	sigtm.val = logr.log_head.tim;

	dbg_print("log tm: %d-%d-%d %d:%d, log len:%d, dat: %s" PTCR,
			  sigtm.time.year, sigtm.time.mon, sigtm.time.day, sigtm.time.hour, sigtm.time.min,
			  logr.log_head.len,
			  buff);

	return 0;
}
DBG_FUN_EXPORT(mix_log_r);

ii08 log_iter_back(mix_log_obj_type *obj);
int mix_log_s(int argc, char **argv)
{
	mix_log_obj_type logr;
	TIME_Type log_stm, log_etm;
	sig_time_type sigtm;
	int min = 1;
	int num;

	uu8 buff[128];

	if (argc < 3)
		return 1;

	if (argc < 7)
		return 1;

	log_stm.year = atoi(argv[1]);
	log_stm.mon = atoi(argv[2]);
	log_stm.day = atoi(argv[3]);
	log_stm.hour = atoi(argv[4]);
	log_stm.min = atoi(argv[5]);
	log_stm.sec = 0;
	min = atoi(argv[6]);

	memcpy(&log_etm, &log_stm, sizeof(log_stm));
	rtc_time_run_min(&log_etm, min);

	logr.dat = buff;
	num = mix_log_obj.api->time_iter(time_to_sig(&log_stm).val, time_to_sig(&log_etm).val, MIX_FLAG_ST_2, log_iter_back, &logr);

	dbg_print("time iter %d" PTCR, num);

	return 0;
}
DBG_FUN_EXPORT(mix_log_s);

int mix_log_ms(int argc, char **argv)
{
	TIME_Type log_stm, log_etm;
	sig_time_type sigtm;
	int min = 1;
	int num;

	if (argc < 3)
		return 1;

	if (argc < 7)
		return 1;

	log_stm.year = atoi(argv[1]);
	log_stm.mon = atoi(argv[2]);
	log_stm.day = atoi(argv[3]);
	log_stm.hour = atoi(argv[4]);
	log_stm.min = atoi(argv[5]);
	log_stm.sec = 0;
	min = atoi(argv[6]);

	memcpy(&log_etm, &log_stm, sizeof(log_stm));
	rtc_time_run_min(&log_etm, min);

	num = mix_log_obj.api->m_time_iter(time_to_sig(&log_stm).val, time_to_sig(&log_etm).val, MIX_FLAG_ST_2, log_iter_back);

	dbg_print("time iter %d" PTCR, num);

	return 0;
}
DBG_FUN_EXPORT(mix_log_ms);

ii08 log_iter_back(mix_log_obj_type *obj)
{
	uu08 dattail;
	sig_time_type sigtm;

	sigtm.val = obj->log_head.tim;
	dattail = obj->dat[obj->log_head.len];
	obj->dat[obj->log_head.len] = 0;
	dbg_print("log tm: %d-%d-%d %d:%d, b=%3d, s=%3d, flat=%2d, log len:%3d, dat: %s" PTCR,
			  sigtm.time.year, sigtm.time.mon, sigtm.time.day, sigtm.time.hour, sigtm.time.min,
			  obj->page_site, obj->sub_site, obj->log_head.flag,
			  obj->log_head.len,
			  (char *)obj->dat);

	obj->dat[obj->log_head.len] = dattail;

	switch (obj->log_head.flag)
	{
	case MIX_FLAG_ST_0:
		mix_log_flag_set(&mix_log_obj, obj, MIX_FLAG_ST_1);
		break;
	case MIX_FLAG_ST_1:
		mix_log_flag_set(&mix_log_obj, obj, MIX_FLAG_ST_2);
		break;
	default:
		break;
	}

	return 1;
}

int mix_log_rblk(int argc, char **argv)
{
	mix_log_obj_type logr;
	int bs = 0, be = 0;
	UU16 flag = 0xFFFF;
	int num;
	uu8 *buff = (uu8 *)malloc(4096 + 4);

	if (argc < 3)
		return 1;

	if (argc >= 4)
		flag = atoi(argv[3]);

	bs = atoi(argv[1]);
	be = atoi(argv[2]);

	logr.dat = buff;
	num = mix_log_obj.api->blk_iter(bs, be, flag, log_iter_back, &logr);

	dbg_print("block iter %d" PTCR, num);
	free(buff);
	return 0;
}
DBG_FUN_EXPORT(mix_log_rblk);

int mix_log_rsit(int argc, char **argv)
{
	mix_log_obj_type logr;
	mix_log_obj_type start;
	mix_log_obj_type end;
	int num;
	UU16 flag = 0xFFFF;

	uu8 buff[128];

	if (argc < 5)
		return 1;

	if (argc >= 6)
		flag = atoi(argv[5]);

	start.page_site = atoi(argv[1]);
	start.sub_site = atoi(argv[2]);

	end.page_site = atoi(argv[3]);
	end.sub_site = atoi(argv[4]);

	logr.dat = buff;
	num = mix_log_obj.api->site_iter(&start, &end, flag, log_iter_back, &logr);

	dbg_print("site iter %d" PTCR, num);

	return 0;
}
DBG_FUN_EXPORT(mix_log_rsit);

int mix_log_mrsit(int argc, char **argv)
{
	mix_log_obj_type start;
	mix_log_obj_type end;
	int num;
	UU16 flag = 0xFFFF;

	uu8 buff[128];

	if (argc < 5)
		return 1;

	if (argc >= 6)
		flag = atoi(argv[5]);

	start.page_site = atoi(argv[1]);
	start.sub_site = atoi(argv[2]);

	end.page_site = atoi(argv[3]);
	end.sub_site = atoi(argv[4]);

	num = mix_log_obj.api->m_site_iter(&start, &end, flag, log_iter_back);

	dbg_print("site iter %d" PTCR, num);

	return 0;
}
DBG_FUN_EXPORT(mix_log_mrsit);

int mix_log_move(int argc, char **argv)
{
	int move, real;
	mix_log_obj_type logr;

	if (argc < 4)
		return 1;

	logr.page_site = atoi(argv[1]);
	logr.sub_site = atoi(argv[2]);
	move = atoi(argv[3]);

	real = mix_log_site_move(&mix_log_obj, &logr, move);

	dbg_print("log move : %d, des blk=%d,site=%d" PTCR, real, logr.page_site, logr.sub_site);

	return 0;
}
DBG_FUN_EXPORT(mix_log_move);

int mix_log_site_chk(int argc, char **argv)
{
	int real;
	mix_log_obj_type logr;

	if (argc < 3)
		return 1;

	logr.page_site = atoi(argv[1]);
	logr.sub_site = atoi(argv[2]);

	real = mix_log_is_site_valid(&mix_log_obj, &logr);

	dbg_print("log site : %s" PTCR, real ? "ok" : "error");

	return 0;
}
DBG_FUN_EXPORT(mix_log_site_chk);

int mix_log_tm(int argc, char **argv)
{
	if (argc < 6)
		return 1;

	log_tm.year = atoi(argv[1]);
	log_tm.mon = atoi(argv[2]);
	log_tm.day = atoi(argv[3]);
	log_tm.hour = atoi(argv[4]);
	log_tm.min = atoi(argv[5]);
	log_tm.sec = 0;

	dbg_print("log time" PTCR);

	return 0;
}
DBG_FUN_EXPORT(mix_log_tm);
